<template>
	<view class="content" style="text-align: center;">
		<image mode="aspectFit" @click="heserclick()" style="width: 400rpx; height: 400rpx;" :src="imgsrc"></image>
		<uni-easyinput v-model="name" placeholder="名称"></uni-easyinput>
		<button @click="save()">保存</button>
		<button @click="setpsw()">设置密码</button>
		<button @click="loginpsw()">密码登录</button>
		<view>请使用主机设置密码</view>
		<view>密码设置之后只能用一次</view>
	</view>
	<view>
		<!-- 输入框示例 -->
		<uni-popup ref="inputDialog" type="dialog">
			<uni-popup-dialog ref="inputClose" mode="input" title="输入密码" placeholder="请输入密码"
				@confirm="dialogInputConfirm"></uni-popup-dialog>
		</uni-popup>
	</view>
	<view>
		<!-- 输入框示例 -->
		<uni-popup ref="login" type="dialog">
			<uni-popup-dialog ref="inputClose" mode="input" title="输入密码" placeholder="请输入密码"
				@confirm="loginpswConfirm"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'user',
				name: '',
				imgsrc: '/static/logo.png',
				FilePath: ''
			}
		},
		onLoad() {
			this.getuser()
		},
		methods: {
			async heserclick() {
				let that = this
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: function(res) {
						that.imgsrc = res.tempFilePaths[0]
						that.upload()
					}
				});
			},
			async save() {
				uni.showLoading({
					title: '保存中',
					mask: true
				})
				let res = await this.HttpTool.UpdateUser(this, {
					name: this.name,
					deviceid: this.Device.deviceId,
					img: this.FilePath
				})
				uni.hideLoading()
				if (res.code !== 200) {
					uni.showToast({
						title: '失败',
						duration: 2000,
					});
				}
				this.getuser()
			},
			async getuser() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				let res = await this.HttpTool.UserLo(this, {
					deviceid: this.Device.deviceId
				})
				uni.hideLoading()
				if (res.code !== 200) {
					uni.showToast({
						icon: 'error',
						title: '加载中失败',
						duration: 2000,
					});
					return
				}
				this.name = res.data.name
				this.imgsrc = this.HttpTool.getUrl() + res.data.img
				this.FilePath = res.data.img
			},
			async upload() {
				uni.showLoading({
					title: '上传中',
					mask: true
				});
				let res = await this.HttpTool.UpUserImg(this, this.imgsrc)
				uni.hideLoading();
				res = JSON.parse(res)
				if (res.code !== 200) {
					uni.showToast({
						icon: 'error',
						title: '上传失败',
						duration: 2000,
					});
					return
				} else {
					uni.showToast({
						title: '上传成功',
						duration: 2000,
					});
				}
				this.FilePath = res.data
			},
			setpsw(){
				this.$refs.inputDialog.open()
			},
			loginpsw(){
				this.$refs.login.open()
			},
			async dialogInputConfirm(val) {
				uni.showLoading({
					title: '设置密码'
				})
				let res = await this.HttpTool.SetPsw(this, {
					deviceid: this.Device.deviceId,
					id:val
				})
				uni.hideLoading()
				if (res.code !== 200) {
					uni.showToast({
						icon: 'error',
						title: '失败',
						duration: 2000,
					});
				}
			},
			async loginpswConfirm(val) {
				uni.showLoading({
					title: '登录'
				})
				let res = await this.HttpTool.SetOtherDevice(this, {
					deviceid: this.Device.deviceId,
					id:val
				})
				uni.hideLoading()
				if (res.code !== 200) {
					uni.showToast({
						icon: 'error',
						title: '失败',
						duration: 2000,
					});
				}else{
					this.getuser()
				}
			},
		}
	}
</script>

<style>
</style>
